package com.hbnu.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class Jwt {
    private static long time = 1000*60*60*24*24;
    private static String key = "yz-32-character-long-secret-key-123456";
    public static String createJwt(Long id,String email){
        return Jwts.builder()
                .claim("id", id)
                .claim("email", email)
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                .setId(UUID.randomUUID().toString())
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + time))
                .signWith(SignatureAlgorithm.HS256, key)
                .compact();
    }

    public static Map<String, Object> parseJwt(String token){
        Claims body = Jwts.parser()
                .setSigningKey(key)
                .parseClaimsJws(token)
                .getBody();
        Map<String, Object> map = new HashMap<>();
        map.put("id", body.get("id"));
        map.put("email", body.get("email"));
        return map;
    }
}
